﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_CustomContainerField:ContainerBase
    {
        private string _title = "";
        private int _labelWidth = 2;
        private string _controlWidth = "";
        private bool _hasError = false;
        private string _errorMsg = "";
        private string _errorValidateName = "";
        private bool _isRequired = false;

        public PageForm_CustomContainerField(PageForm_Form form, ViewContext viewContext) : base(viewContext)
        {
            this.Css("form-group webui-form_formfield webui-form_customcontianerfield");
        }

        /// <summary>
        /// 使用指定name的错误信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PageForm_CustomContainerField WithNameError(string name)
        {
            _errorValidateName = name;
            return this;
        }

        /// <summary>
        /// 直接输出错误信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public PageForm_CustomContainerField ErrorMessage(string message)
        {
            //_hasError = !string.IsNullOrWhiteSpace(message);

            if (!string.IsNullOrWhiteSpace(message))
            {
                _hasError = true;

                _errorMsg = (_errorMsg + "|" + message).Trim('|');
            }
            
            

            return this;
        }

        public PageForm_CustomContainerField Label(string title)
        {
            _title = title;

            return this;
        }

        public PageForm_CustomContainerField LabelWidth(int labelWidth)
        {
            _labelWidth = labelWidth;

            return this;
        }

        public PageForm_CustomContainerField ControlWidth(string controlWidth)
        {
            _controlWidth = controlWidth;

            return this;
        }

        public PageForm_CustomContainerField Required(bool isRequired = true)
        {
            _isRequired = isRequired;

            return this;
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div>");

            if (_hasError)
            {
                ViewContext.Writer.WriteLine($"<span class=\"help-block\">{_errorMsg}</span>");
            }

            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");
            

            ViewContext.Writer.WriteLine("</div> <!-- end control-content -->");

            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

            ViewContext.Writer.WriteLine("</div> <!--end webui-form_formfield -->");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }
        }

        internal override ContainerBase RenderInternal()
        {
            if (!string.IsNullOrWhiteSpace(_errorValidateName) && ViewContext.ModelState.TryGetValue(_errorValidateName, out var error)) // ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);
            {
                ErrorMessage(error.Errors.Select(x => x.ErrorMessage).JoinToString('|'));
            }

            if (!_hasError)
            {
                _hasError = !string.IsNullOrWhiteSpace(_errorMsg);
            }

            if (_hasError)
            {
                this.Css("has-error");
            }

            this.Css("webui-form_requiredfield");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            if (!string.IsNullOrEmpty(_title))
            {
                ViewContext.Writer.WriteLine($"<label class=\"col-sm-{_labelWidth} control-label\">{(_isRequired ? "<i class=\"fa fa-fw fa-asterisk requiredfield_label\"></i>" : "")}{_title}</label>");
            }

            
            
            ViewContext.Writer.WriteLine($"<div class=\"col-sm-{(string.IsNullOrWhiteSpace(_title) ? 12 : 12 - _labelWidth)} control-content\">");

            ViewContext.Writer.WriteLine($"<div class=\"input-group row\" style=\"{(string.IsNullOrWhiteSpace(_controlWidth) ? "" : $"width:{_controlWidth} !important;")}\">");
            
            return this;
        }
    }
}
